package org.apache.james.mime4j.decoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.mime4j.util.CharArrayBuffer;
import org.apache.james.mime4j.util.CharsetUtil;

public class DecoderUtil
{
  static Class class$org$apache$james$mime4j$decoder$DecoderUtil;
  private static Log log;

  static
  {
    Class localClass;
    if (class$org$apache$james$mime4j$decoder$DecoderUtil == null)
    {
      localClass = class$("org.apache.james.mime4j.decoder.DecoderUtil");
      class$org$apache$james$mime4j$decoder$DecoderUtil = localClass;
    }
    while (true)
    {
      log = LogFactory.getLog(localClass);
      return;
      localClass = class$org$apache$james$mime4j$decoder$DecoderUtil;
    }
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  public static String decodeB(String paramString1, String paramString2)
    throws UnsupportedEncodingException
  {
    return new String(decodeBase64(paramString1), paramString2);
  }

  public static byte[] decodeBase64(String paramString)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      Base64InputStream localBase64InputStream = new Base64InputStream(new ByteArrayInputStream(paramString.getBytes("US-ASCII")));
      int i = localBase64InputStream.read();
      if (i != -1)
        localByteArrayOutputStream.write(i);
    }
    catch (IOException localIOException)
    {
      log.error(localIOException);
      return localByteArrayOutputStream.toByteArray();
    }
  }

  public static byte[] decodeBaseQuotedPrintable(String paramString)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      QuotedPrintableInputStream localQuotedPrintableInputStream = new QuotedPrintableInputStream(new ByteArrayInputStream(paramString.getBytes("US-ASCII")));
      int i = localQuotedPrintableInputStream.read();
      if (i != -1)
        localByteArrayOutputStream.write(i);
    }
    catch (IOException localIOException)
    {
      log.error(localIOException);
      return localByteArrayOutputStream.toByteArray();
    }
  }

  public static String decodeEncodedWords(String paramString)
  {
    int j;
    label51: int k;
    int l;
    int i1;
    Object localObject;
    CharArrayBuffer localCharArrayBuffer = new CharArrayBuffer(128);
    int i = 0;
    try
    {
      if (i < paramString.length())
      {
        j = paramString.indexOf("=?", i);
        if (j != -1)
          break label51;
        localCharArrayBuffer.append(paramString.substring(i));
      }
      return localCharArrayBuffer.toString();
      if (j - i > 0)
        localCharArrayBuffer.append(paramString.substring(i, j));
      k = paramString.indexOf(63, j + 2);
      if (k != -1)
      {
        l = paramString.indexOf(63, k + 1);
        if (l == -1)
          break label218;
        i1 = paramString.indexOf("?=", l + 1);
        if (i1 != -1)
          break label526;
        if (l == -1)
          break label225;
        if (paramString.length() - 1 == l)
          break label522;
        if (paramString.charAt(l + 1) != '=')
          break label225;
        break label522:
        i = i1 + 2;
        label522: label526: label427: label218: label350: label225: localCharArrayBuffer.append((String)localObject);
      }
    }
    catch (Throwable localThrowable)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
                log.error("Decoding header field body '" + paramString + "'", localThrowable);
              l = -1;
            }
            i1 = -1;
          }
          localCharArrayBuffer.append(paramString.substring(j));
        }
        do
        {
          while (true)
          {
            String str2;
            do
            {
              String str3;
              while (true)
              {
                String str4;
                while (true)
                {
                  while (true)
                  {
                    String str1;
                    do
                    {
                      while (true)
                      {
                        do
                        {
                          str1 = paramString.substring(j + 2, k);
                          str2 = paramString.substring(k + 1, l);
                          str3 = paramString.substring(l + 1, i1);
                          str4 = CharsetUtil.toJavaCharset(str1);
                          if (str4 != null)
                            break label350;
                          localObject = paramString.substring(j, i1 + 2);
                        }
                        while (!(log.isWarnEnabled()));
                        log.warn("MIME charset '" + str1 + "' in header field doesn't have a " + "corresponding Java charset");
                      }
                      if (CharsetUtil.isDecodingSupported(str4))
                        break label427;
                      localObject = paramString.substring(j, i1 + 2);
                    }
                    while (!(log.isWarnEnabled()));
                    log.warn("Current JDK doesn't support decoding of charset '" + str4 + "' (MIME charset '" + str1 + "')");
                  }
                  if (!(str2.equalsIgnoreCase("Q")))
                    break;
                  localObject = decodeQ(str3, str4);
                }
                if (!(str2.equalsIgnoreCase("B")))
                  break;
                localObject = decodeB(str3, str4);
              }
              localObject = str3;
            }
            while (!(log.isWarnEnabled()));
            log.warn("Warning: Unknown encoding in header field '" + str2 + "'");
          }
          i1 = l;
        }
        while (l != i1);
        localObject = "";
      }
    }
  }

  public static String decodeQ(String paramString1, String paramString2)
    throws UnsupportedEncodingException
  {
    CharArrayBuffer localCharArrayBuffer = new CharArrayBuffer(128);
    int i = 0;
    if (i < paramString1.length())
    {
      char c = paramString1.charAt(i);
      if (c == '_')
        localCharArrayBuffer.append("=20");
      while (true)
      {
        while (true)
          ++i;
        localCharArrayBuffer.append(c);
      }
    }
    return new String(decodeBaseQuotedPrintable(localCharArrayBuffer.toString()), paramString2);
  }
}